﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BlogManager.Configuration;
using BlogManager.Services;
using BlogManager.Properties;

namespace BlogManager
{
    public partial class BackupForm : Form
    {
        public BackupForm()
        {
            InitializeComponent();
        }

        private void actOk_Update(object sender, EventArgs e)
        {
            int n;
            actOk.Enabled = int.TryParse(cmbNumberOfPosts.Text, out n) && n > 0;
        }

        private void actOk_Execute(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = Settings.Default.LastBackupFile;
            int n;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK &&
                int.TryParse(cmbNumberOfPosts.Text, out n))
            {
                IPostService service = ServiceRepository.Factory.GetPostService();
                service.Backup(n, saveFileDialog1.FileName);
                Settings.Default.LastBackupFile = saveFileDialog1.FileName;
                Settings.Default.Save();

                MsgBox.Info(Resources.BackupCorrect);

                this.Close();
            }
        }

        private void cmbNumberOfPosts_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && e.KeyChar != '\b';
        }

    }
}
